package com.tx.springboot_demo.config;

import com.tx.springboot_demo.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ITemplateResolver;

/**
 * @author ：tx
 * @description： jsp和thymeleaf整合
 * @date ：Created in 2019/4/9 14:06
 * @modified By：
 * @version:
 */
@Configuration
public class MercuryViewResolverConfiguration implements WebMvcConfigurer {


        @Bean
        public ViewResolver viewResolver() {
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/jsp");
            resolver.setSuffix(".jsp");
            resolver.setViewNames("*");
            resolver.setOrder(2);
            return resolver;
        }

        @Bean
        public ITemplateResolver templateResolver() {
            SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
            templateResolver.setTemplateMode("HTML5");
            templateResolver.setPrefix("/WEB-INF/");
            templateResolver.setSuffix(".html");
            templateResolver.setCharacterEncoding("utf-8");
            templateResolver.setCacheable(false);
            return templateResolver;
        }

        @Bean
        public SpringTemplateEngine templateEngine() {
            SpringTemplateEngine templateEngine = new SpringTemplateEngine();
            templateEngine.setTemplateResolver(templateResolver());
            // templateEngine
            return templateEngine;
        }

        @Bean
        public ThymeleafViewResolver viewResolverThymeLeaf() {
            ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
            viewResolver.setTemplateEngine(templateEngine());
            viewResolver.setCharacterEncoding("utf-8");
            viewResolver.setOrder(1);
            viewResolver.setViewNames(new String[]{"/html/*","vue/*"});
            return viewResolver;
        }

        @Override
        public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
            configurer.enable();
        }

        /**
         * @author tx
         * @description  存在@EnableWebMvc 默认的会失效
         *        无前缀--> src/main/webapp
         *        有前缀--> src/main/resources
         *        file-->文件系统路径
         *
         * @date  2019/4/9 16:41
         * @param  * @Param: registry
         * @return void
        **/
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
           /* super.addResourceHandlers(registry);*/
            //配置静态资源处理
            registry.addResourceHandler("/**")
                    .addResourceLocations("resources/", "static/", "public/",
                            "META-INF/resources/")
                    .addResourceLocations("classpath:resources/", "classpath:static/",
                            "classpath:public/", "classpath:META-INF/resources/")
                    .addResourceLocations("file:///tmp/webapps/");
        }

        /**
         * @author tx
         * @description  添加访问controller 返回的视图
         *          作用： 不用写controller来跳转到视图了
         * @date  2019/4/9 17:00
         * @param  * @Param: registry
         * @return void
        **/
        /*@Override
        protected void addViewControllers(ViewControllerRegistry registry) {
            super.addViewControllers(registry);
            registry.addViewController("/toLogin").setViewName("/login");
        }*/

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/toLogin").setViewName("/login");
    }

    /**
         * @author tx
         * @description  拦截器配置
         * @date  2019/4/9 20:07
         * @param  * @Param: registry
         * @return void
        **/
       /* @Override
        protected void addInterceptors(InterceptorRegistry registry) {
            super.addInterceptors(registry);
            registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/*").excludePathPatterns("/toLogin","/loginController/login","/js");
        }*/

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/*").excludePathPatterns("/toLogin","/loginController/login","/js");
    }
}